<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Academic Economic Research Report</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Georgia', serif;
            line-height: 1.6;
            color: #333;
        }
        .header {
            border-bottom: 2px solid #0066cc;
            padding-bottom: 15px;
            margin-bottom: 30px;
        }
        .chart-container {
            background-color: #f8f9fa;
            border-radius: 5px;
            padding: 20px;
            margin-bottom: 30px;
        }
        .paper-card {
            border-left: 4px solid #0066cc;
            padding-left: 15px;
            margin-bottom: 20px;
        }
        .paper-title {
            font-weight: bold;
            color: #0066cc;
        }
        @media print {
            .no-print {
                display: none;
            }
            body {
                font-size: 12pt;
            }
        }
    </style>
</head>
<body>
    <div class="container mt-4 mb-5">
        <div class="header">
            <h1>Economic Research Report</h1>
            <p class="lead">Topic: {{ topic|title }} | Country/Region: {{ country }} | Period: {{ years }} years</p>
        </div>

        <div class="chart-container">
            <h3>Economic Data Overview</h3>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Year</th>
                            <th>Value</th>
                            <th>Change</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in data[1] %}
                        <tr>
                            <td>{{ item.date }}</td>
                            <td>{{ item.value }}</td>
                            <td>{% if loop.previtem %}{{ (item.value - loop.previtem.value)|round(2) }}{% else %}N/A{% endif %}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="text-center mt-4">
                <img src="https://quickchart.io/chart?c={type:'line',data:{labels:[{% for item in data[1] %}'{{item.date}}'{% if not loop.last %},{% endif %}{% endfor %}],datasets:[{label:'{{topic}}',data:[{% for item in data[1] %}{{item.value}}{% if not loop.last %},{% endif %}{% endfor %}]}]}}" alt="Economic Trend Chart" class="img-fluid">
            </div>
        </div>

        <div class="academic-papers">
            <h3>Related Academic Papers</h3>
            <div class="papers-list">
                {{ papers.papers|safe }}
            </div>
        </div>

        <div class="simulation-results mt-5">
            <h3>Agent-Based Simulation Results</h3>
            <div class="row">
                <div class="col-md-6">
                    <div class="card mb-4">
                        <div class="card-header">Market Conditions</div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-6">
                                    <img src="https://quickchart.io/chart?c={type:'doughnut',data:{labels:['Inflation','Interest Rate','Unemployment'],datasets:[{data:[{{simulation.inflation|float*100}},{{simulation.interest_rate|float*100}},{{simulation.unemployment|float*100}}],backgroundColor:['#FF6384','#36A2EB','#FFCE56']}]}}" class="img-fluid">
                                </div>
                                <div class="col-6">
                                    <ul class="list-group">
                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                            Inflation
                                            <span class="badge bg-primary rounded-pill">{{ "%.2f"|format(simulation.inflation|float * 100) }}%</span>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                            Interest Rate
                                            <span class="badge bg-primary rounded-pill">{{ "%.2f"|format(simulation.interest_rate|float * 100) }}%</span>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                            Unemployment
                                            <span class="badge bg-primary rounded-pill">{{ "%.2f"|format(simulation.unemployment|float * 100) }}%</span>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">Economic Dynamics</div>
                        <div class="card-body">
                            <img src="https://quickchart.io/chart?c={type:'radar',data:{labels:['Stability','Growth','Risk'],datasets:[{label:'Market Health',data:[{{1-simulation.unemployment|float}},{{1-simulation.inflation|float}},{{1-simulation.interest_rate|float}}],backgroundColor:'rgba(75, 192, 192, 0.2)',borderColor:'rgba(75, 192, 192, 1)'}]}}" class="img-fluid">
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="network-analysis mt-5">
            <h3>Economic Network Analysis</h3>
            <div class="row">
                <div class="col-md-8">
                    <div class="card mb-4">
                        <div class="card-header">Network Structure</div>
                        <div class="card-body">
                            <div id="network-graph" style="height: 400px;"></div>
                            <script src="https://d3js.org/d3.v7.min.js"></script>
                            <script>
                                const networkData = {{ network_data|safe }};
                                const metrics = {{ network_metrics|safe }};
                                
                                // D3.js network visualization code
                                const width = 800, height = 400;
                                const svg = d3.select("#network-graph")
                                    .append("svg")
                                    .attr("width", width)
                                    .attr("height", height);
                                
                                // Add simulation and visualization code here
                                console.log("Network data loaded:", networkData);
                            </script>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">Network Metrics</div>
                        <div class="card-body">
                            <table class="table table-sm">
                                <tbody>
                                    <tr>
                                        <th>Node Count</th>
                                        <td>{{ network_metrics.node_count }}</td>
                                    </tr>
                                    <tr>
                                        <th>Edge Count</th>
                                        <td>{{ network_metrics.edge_count }}</td>
                                    </tr>
                                    <tr>
                                        <th>Avg. Degree</th>
                                        <td>{{ "%.2f"|format(network_metrics.avg_degree) }}</td>
                                    </tr>
                                    <tr>
                                        <th>Systemic Risk</th>
                                        <td>{{ "%.2f"|format(network_metrics.systemic_risk * 100) }}%</td>
                                    </tr>
                                    <tr>
                                        <th>Network Resilience</th>
                                        <td>{{ "%.2f"|format(network_metrics.network_resilience * 100) }}%</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="footer no-print mt-5 text-muted">
            <p>Report generated on {{ now() }} | Market Researcher Tool with Advanced Network Simulation</p>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>